getname ()
getspumber()
windowloads()

function outlogin(){outloginfn()}
var username = getCookie('username')  
var token = window.localStorage.getItem('token')
var reg = /(?!id=)(\w+)$/
var arr =  location.search.match(reg)
var goodsid = arr[1]

$.get('goods/api/detail/'+goodsid).then(res=>{
    $('.img-center')[0].innerHTML = `
    <div class="magnifier">
    </div>
    <img src="${res.data.goodsdata.goodsimg1}" alt="">`

    $('.img-min')[0].innerHTML = `
    <ul>
        <li class = "active"><img src="${res.data.goodsdata.goodsimg1}" alt=""> </li>
        <li><img src="${res.data.goodsdata.goodsimg2}" alt=""> </li>
        <li><img src="${res.data.goodsdata.goodsimg3}" alt=""> </li>
    </ul>
    `
     $('#Commodity-Details h1')[0].innerHTML = `
        ${res.data.goodsdata.goodsname}
     `
     setStyle($('.img-max')[0],{
        'background-image' : `url(${res.data.goodsdata.goodsimg1})`,
        'background-size' : '1600px 1600px',
        'background-repeat': 'no-repeat'
    })
     addcart()
     magnifier()
})

// 放大镜
function magnifier(){
    var magnifier = $('.magnifier')[0]
    var mins =  $('.img-min')[0].children[0].children
    for(var i =0; i<mins.length;i++){
       mins[i].onclick = function(){
            $('.img-center')[0].children[1].src = this.children[0].src
        setStyle($('.img-max')[0],{
            'background-image' : `url(${this.children[0].src})`,
        })
       }
    }
    $('.img-center')[0].onmouseover = function(){
        $('.img-max')[0].style.display = 'block'
        magnifier.style.display = 'block'
        $('.img-center')[0].onmousemove = function(){
             var e = window.event
             var x = e.pageX
             var y = e.pageY
             var l = x - $('.img-center')[0].parentElement.parentElement.offsetLeft -magnifier.offsetWidth/2
             var t = y -$('.img-center')[0].parentElement.parentElement.offsetTop - magnifier.offsetHeight/2
             if (l < 0) {
                 l = 0
             }
             if (t < 0) {
                 t = 0
             }
             if (l > $('.img-center')[0].clientWidth -magnifier.offsetWidth) {
                 l = $('.img-center')[0].clientWidth -magnifier.offsetWidth
             }
             if (t > $('.img-center')[0].clientHeight -magnifier.offsetHeight) {
                 t = $('.img-center')[0].clientHeight -magnifier.offsetHeight
             }
            magnifier.style.left = l + 'px'
            magnifier.style.top = t + 'px'
            $('.img-max')[0].style.backgroundPosition = -l*4  + 'px ' + -t *4 + 'px' 
        }
            
    }   
    $('.img-center')[0].onmouseout =  function() {  
        $('.img-max')[0].style.display = 'none'
        magnifier.style.display = 'none'
    }

}

var buyBox = document.querySelector('#locabox')
buyBox.onclick = function(){
    if(!username){
        layer.msg('请先登录',{
            icon:2,
            time:2000
        },function(){
            window.localStorage.setItem('url',location.href)
            location.href = '/login.html'
        })
        return false
    }
    var userid = getCookie('userid')
    var goodsnumber = 1
    $.ajax('cart/api/add',{
        method: 'post',
        headers:{
            'Authorization':token
        },
        data:{
            goodsid,
            userid,
            goodsnumber
        }
    })

}

// 添加购物车
function addcart(){
    $('.shopping-cart').click(function(){
        if(!username){
            layer.msg('请先登录',{
                icon:2,
                time:2000
            },function(){
                window.localStorage.setItem('url',location.href)
                location.href = '/login.html'
            })
            return false
        }

        if(!token){
            layer.msg('请先登录',{
                icon:2,
                time:2000
            },function(){
                window.localStorage.setItem('url',location.href)
                location.href = '/login.html'
            })
            return false
        }
        addone()
    })         
}

function addone(){
        var userid = getCookie('userid')
        var goodsnumber = 1
        $.ajax('cart/api/add',{
            method: 'post',
            headers:{
                'Authorization':token
            },
            data:{
                goodsid,
                userid,
                goodsnumber
            }
        }).then(res=>{
            getspumber()
            if(res.errorCode == 0){
                layer.msg('添加购物车成功！',{
                    icon:1,
                    time:2000
                })
                }else{
                layer.msg('添加购物车失败！',{
                    icon:2,
                    time:2000
                })
            }
        })
}